/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.core.analyzer;

import org.open.cobertura.data.CoberturaCoverageCounter;
import org.open.cobertura.data.ICoberturaClassCovered;
import org.open.cobertura.data.ICoberturaLineCovered;
import org.open.cobertura.data.ICoberturaMethodCovered;

import java.util.ArrayList;
import java.util.List;

/**
 * 类覆盖率
 */
public class CoberturaClassCoveredImpl extends CoberturaSourceCoveredImpl implements ICoberturaClassCovered {

    private List<ICoberturaMethodCovered> coberturaMethodCovered;
    protected CoberturaCoverageCounter classCounter;
    public CoberturaClassCoveredImpl() {
        this.coberturaMethodCovered=new ArrayList<>();
        this.coberturaLineCovered=new ArrayList<>();
        this.classCounter=new CoberturaCoverageCounter();
        this.methodCounter=new CoberturaCoverageCounter();
        this.branchCounter=new CoberturaCoverageCounter();
        this.lineCounter=new CoberturaCoverageCounter();
        this.instructionCounter=new CoberturaCoverageCounter();

    }

    /**
     * 类名
     */
    private String name;

    /**
     * 包名
     */
    private String packageName;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getPackageName() {
        return packageName;
    }

    @Override
    public String getClassName(){
        return this.name;
    }

    /**
     * 计算类的覆盖率
     * @param coberturaMethodCovered
     */
    public void increase(CoberturaMethodCoveredImpl coberturaMethodCovered){
        this.coberturaMethodCovered.add(coberturaMethodCovered);
        super.increase(coberturaMethodCovered);
        this.increaseClassCoverage(coberturaMethodCovered);
    }

    private void increaseClassCoverage(CoberturaMethodCoveredImpl coberturaMethodCovered){
        if(coberturaMethodCovered.getMethodCounter().getCoveredCount()>0) {
            this.classCounter.coveredCount=1;
            this.classCounter.missedCount=0;
            this.classCounter.setTotalCount(this.classCounter.missedCount+this.classCounter.coveredCount);
            this.methodCounter.coveredCount+=1;
            this.methodCounter.setTotalCount(this.methodCounter.coveredCount+this.methodCounter.missedCount);
        }else{
            this.classCounter.coveredCount=0;
            this.classCounter.missedCount=1;
            this.classCounter.setTotalCount(this.classCounter.missedCount+this.classCounter.coveredCount);
            this.methodCounter.missedCount+=1;
            this.methodCounter.setTotalCount(this.methodCounter.coveredCount+this.methodCounter.missedCount);
        }
    }
    /**
     * 得到方法覆盖率数据
     * @return
     */
    @Override
    public List<ICoberturaMethodCovered> getCoberturaMethodCovered() {
        return coberturaMethodCovered;
    }

    @Override
    public CoberturaCoverageCounter getInstructionCounter() {
        return super.getInstructionCounter();
    }

    @Override
    public CoberturaCoverageCounter getBranchCounter() {
        return super.getBranchCounter();
    }

    @Override
    public CoberturaCoverageCounter getLineCounter() {
        return super.getLineCounter();
    }

    @Override
    public CoberturaCoverageCounter getComplexityCounter() {
        return super.getComplexityCounter();
    }

    @Override
    public CoberturaCoverageCounter getMethodCounter() {
        return super.getMethodCounter();
    }


    public CoberturaCoverageCounter getClassCounter() {
        return this.classCounter;
    }

    @Override
    public List<ICoberturaLineCovered> getCoberturaLineCovered() {
        return this.coberturaLineCovered;
    }

}
